//
//  PayAccountInfoView.m
//  CedarFinancePH
//
//  Created by menchao on 2018/8/2.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "PayAccountInfoView.h"



@interface PayAccountInfoView()

@property (nonatomic,strong) ProductPayInfoRsp *productPayInfo;

@property (nonatomic,strong) UILabel *timeLbl;
@property (nonatomic,strong) UILabel *accountLbl;
@property (nonatomic,strong) UILabel *cardLbl;
@property (nonatomic,strong) UILabel *bankLbl;

@property (nonatomic,strong) UIImageView *imageV;

@end

@implementation PayAccountInfoView

- (instancetype)initWithProductPayInfo:(ProductPayInfoRsp *)productPayInfo{
    if (self = [super init]) {
//        self.backgroundColor = kWhiteColor;
        self.productPayInfo = productPayInfo;
        [self initUI:self];
        [self updatePayAccountInfo:productPayInfo];
    }
    return self;
}

- (void)updatePayAccountInfo:(ProductPayInfoRsp *)productPayInfo{
    if (productPayInfo) {
        NSString *timeStr;
        double minute = [productPayInfo.minute doubleValue];
        if (minute <= 0) {
            if(IsEmptyStr(productPayInfo.payHour)){
                productPayInfo.payHour = @"24";
            }
            timeStr = [NSString stringWithFormat:@"请于%@年%@月%@日%@点前打款到如下指定账户:",productPayInfo.payYear,productPayInfo.payMonth,productPayInfo.payDay,productPayInfo.payHour];
        }else{
            timeStr = [NSString stringWithFormat:@"请于%@年%@月%@日%@点%@分前打款到如下指定账户:",productPayInfo.payYear,productPayInfo.payMonth,productPayInfo.payDay,productPayInfo.payHour,productPayInfo.minute];
        }
        NSMutableAttributedString *timeAttributedStr = [[NSMutableAttributedString alloc] initWithString:timeStr];
        NSDictionary *attrDict = @{NSForegroundColorAttributeName:kBlueColor};
        //找年
        NSRange range0 =  [CedarTools findString:[timeAttributedStr string] start:@"请于" end:@"年"];
        [timeAttributedStr addAttributes:attrDict range:range0];
        
        NSRange range1 =  [CedarTools findString:[timeAttributedStr string] start:@"年" end:@"月"];
        [timeAttributedStr addAttributes:attrDict range:range1];
        
        NSRange range2 =  [CedarTools findString:[timeAttributedStr string] start:@"月" end:@"日"];
        [timeAttributedStr addAttributes:attrDict range:range2];
        
        NSRange range3 =  [CedarTools findString:[timeAttributedStr string] start:@"日" end:@"点"];
        [timeAttributedStr addAttributes:attrDict range:range3];
        if (minute >0){
            NSRange range4 =  [CedarTools findString:[timeAttributedStr string] start:@"点" end:@"分"];
            [timeAttributedStr addAttributes:attrDict range:range4];
        }
        self.timeLbl.attributedText = timeAttributedStr;
        self.accountLbl.text = productPayInfo.accountName;
        self.cardLbl.text = productPayInfo.accountNo;
        self.bankLbl.text = productPayInfo.accountBank;
    }
}

- (void)initUI:(UIView *)superView{
    UILabel *tip1Lbl = [[UILabel alloc]init];
    MACRO_UILabel_Configure(tip1Lbl, @"请于年月日点前打款到如下指定账户", kBlackColor, 15)
    self.timeLbl = tip1Lbl;
    [superView addSubview:tip1Lbl];
    [tip1Lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView).mas_offset(20);
        make.left.mas_equalTo(superView).mas_offset(kMarginOffsetX);
        make.right.mas_equalTo(superView).mas_offset(-kMarginOffsetX);
    }];
    
    [superView addSubview:self.imageV];
    [self.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.mas_equalTo(tip1Lbl.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(superView).mas_offset(kMarginOffsetX);
        make.right.mas_equalTo(superView).mas_offset(-kMarginOffsetX);
        make.height.equalTo(@150);
    }];
    
    CGFloat  marginTopBottom = 15.0;//上下间距
    CGFloat  lineWidth = 1.5;//线条宽度
    MACRO_UIView(hLine1View,kMasterColor)
    [superView addSubview:hLine1View];
    hLine1View.hidden = YES;
    [hLine1View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tip1Lbl.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(tip1Lbl);
        make.right.mas_equalTo(superView).mas_offset(-kMarginOffsetX);
        make.height.mas_offset(3);
    }];
    MACRO_UIView(hLine2View,kMasterColor)
    [superView addSubview:hLine2View];
    [hLine2View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tip1Lbl.mas_bottom).mas_offset(20);
        make.left.right.mas_equalTo(hLine1View);
        make.height.mas_offset(lineWidth);
    }];
    hLine2View.hidden = YES;
    
    //账户名称
    UILabel *accountTitleLbl = [[UILabel alloc]init];
    MACRO_UILabel_Configure(accountTitleLbl, @"账户名称", kWhiteColor, 15)
    [superView addSubview:accountTitleLbl];
    [accountTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tip1Lbl.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(tip1Lbl.mas_left).mas_offset(kMarginOffsetX);
        make.width.mas_offset(62);
    }];
    
    UILabel *tip1Lb2 = [[UILabel alloc]init];
    MACRO_UILabel_Configure(tip1Lb2, @"", kWhiteColor, 14)
    self.accountLbl = tip1Lb2;
    tip1Lb2.numberOfLines = 0;
    tip1Lb2.lineBreakMode =  NSLineBreakByWordWrapping;
    [superView addSubview:tip1Lb2];
    [tip1Lb2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(accountTitleLbl.mas_right).mas_offset(12);
        make.centerY.mas_equalTo(accountTitleLbl);
        make.right.mas_equalTo(superView).mas_offset(-60);
    }];
    
    CedarButton *copy1Btn =  [CedarButton  buttonWithImageName:@"copy"];
    [copy1Btn PFCGStyleFontSize:15];
    copy1Btn.clickEvent = ^{
        if (self.productPayInfo.accountName) {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            [CedarTipsManager showTipsInWindowWithOnlyText:@"账户名称复制成功"];
            pasteboard.string = self.productPayInfo.accountName;
        }
    };
    [superView addSubview:copy1Btn];
    [copy1Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(superView).mas_offset(-kMarginOffsetX-10);
        make.centerY.mas_equalTo(accountTitleLbl);
        make.width.equalTo(@12);
        make.height.equalTo(@13);
    }];
    
    //卡号
    UILabel *cardLbl = [[UILabel alloc]init];
    MACRO_UILabel_Configure(cardLbl, @"账  号", kWhiteColor, 15)
    [superView addSubview:cardLbl];
    [cardLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(accountTitleLbl.mas_bottom).mas_offset(marginTopBottom*2+lineWidth);
        make.left.mas_equalTo(accountTitleLbl);
        make.width.mas_offset(62);
    }];
    
    UILabel *tip1Lb3 = [[UILabel alloc]init];
    MACRO_UILabel_Configure(tip1Lb3, @"", kWhiteColor, 15)
    self.cardLbl = tip1Lb3;
    tip1Lb3.numberOfLines = 0;
    tip1Lb3.lineBreakMode =  NSLineBreakByWordWrapping;
    [superView addSubview:tip1Lb3];
    [tip1Lb3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(cardLbl.mas_right).mas_offset(12);
        make.centerY.mas_equalTo(cardLbl);
        make.right.mas_equalTo(superView).mas_offset(-60);
    }];
    
    CedarButton *copy2Btn =   [CedarButton  buttonWithImageName:@"copy"];
    [copy2Btn PFCGStyleFontSize:15];
    copy2Btn.clickEvent = ^{
        if (self.productPayInfo.accountNo) {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            [CedarTipsManager showTipsInWindowWithOnlyText:@"账号复制成功"];
            pasteboard.string = self.productPayInfo.accountNo;
        }
    };
    [superView addSubview:copy2Btn];
    [copy2Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(superView).mas_offset(-kMarginOffsetX - 10);
        make.centerY.mas_equalTo(cardLbl);
        make.width.equalTo(@12);
        make.height.equalTo(@13);
    }];
    
    //银行
    UILabel *bankLbl = [[UILabel alloc]init];
    MACRO_UILabel_Configure(bankLbl, @"开户银行", kWhiteColor, 15)
    [superView addSubview:bankLbl];
    [bankLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(cardLbl.mas_bottom).mas_offset(marginTopBottom*2+lineWidth);
        make.left.mas_equalTo(accountTitleLbl);
        make.width.mas_offset(62);
    }];
    
    UILabel *tip1Lb4 = [[UILabel alloc]init];
    tip1Lb4.numberOfLines = 0;
    tip1Lb4.lineBreakMode =  NSLineBreakByWordWrapping;
    self.bankLbl = tip1Lb4;
    MACRO_UILabel_Configure(tip1Lb4, @"", kWhiteColor, 14)
    [superView addSubview:tip1Lb4];
    [tip1Lb4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(bankLbl);
        make.right.mas_equalTo(superView).mas_offset(-60);
        make.left.mas_equalTo(bankLbl.mas_right).mas_offset(12);
    }];
    
    CedarButton *copy3Btn =   [CedarButton  buttonWithImageName:@"copy"];
    [copy3Btn PFCGStyleFontSize:15];
    [superView addSubview:copy3Btn];
    @weakify(self)
    copy3Btn.clickEvent = ^{
        @strongify(self)
        if (self.productPayInfo.accountBank) {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            [CedarTipsManager showTipsInWindowWithOnlyText:@"开户银行复制成功"];
            pasteboard.string = self.productPayInfo.accountBank;
        }
    };
    [superView addSubview:copy3Btn];
    [copy3Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(superView).mas_offset(-kMarginOffsetX - 10);
        make.centerY.mas_equalTo(bankLbl);
        make.width.equalTo(@12);
        make.height.equalTo(@13);
    }];

    //添加横线
    MACRO_UIView(hLine3View,kWhiteColor)
    [superView addSubview:hLine3View];
    [hLine3View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(accountTitleLbl.mas_bottom).mas_offset(marginTopBottom);
        make.left.right.mas_equalTo(hLine1View);
        make.height.mas_offset(lineWidth);
    }];
    
    MACRO_UIView(hLine4View,kWhiteColor)
    [superView addSubview:hLine4View];
    [hLine4View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(cardLbl.mas_bottom).mas_offset(marginTopBottom);
        make.left.right.mas_equalTo(hLine1View);
        make.height.mas_offset(lineWidth);
    }];
    
    MACRO_UIView(hLine5View,kMasterColor)
    [superView addSubview:hLine5View];
    hLine5View.hidden = YES;
    [hLine5View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bankLbl.mas_bottom).mas_offset(marginTopBottom);
        make.left.right.mas_equalTo(hLine1View);
        make.height.mas_offset(lineWidth);
        //make.width.mas_offset(40);
    }];
    
    //竖线
    MACRO_UIView(vLine1View,kWhiteColor)
    [superView addSubview:vLine1View];
    [vLine1View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.imageV);
        make.bottom.mas_equalTo(hLine5View);
        make.left.mas_equalTo(accountTitleLbl.mas_right).mas_offset(5);
        make.width.mas_offset(lineWidth);
    }];
    MACRO_UIView(vLine2View,kMasterColor)
    [superView addSubview:vLine2View];
    vLine2View.hidden = YES;
    [vLine2View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(hLine2View);
        make.bottom.mas_equalTo(hLine5View);
        make.right.mas_equalTo(superView).mas_offset(-60+lineWidth);
        make.width.mas_offset(lineWidth);
    }];
    
    
    //底部提示
    UILabel *tip2Lbl = [[UILabel alloc]init];
    MACRO_UILabel_Configure(tip2Lbl, @"如在规定时间内未收到您的打款,您的预约将被视为无效", kGrayColor, 14)
    [superView addSubview:tip2Lbl];
    [tip2Lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bankLbl.mas_bottom).mas_offset(30);
        make.left.mas_equalTo(tip1Lbl);
    }];
    
    UILabel *tip3Lbl = [[UILabel alloc]init];
    MACRO_UILabel_Configure(tip3Lbl, @"请务必使用绑定的银行卡进行线下打款!", kBlueColor, 14)
    [superView addSubview:tip3Lbl];
    [tip3Lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tip2Lbl.mas_bottom).mas_offset(5);
        make.left.mas_equalTo(tip1Lbl);
    }];
}


MACRO_UIImageView(imageV,@"account")


@end
